/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmltest;

import ParTreeVisualizer.ParallelTreeVisualizer;
import ParTreeVisualizer.SentenceAlignement;
import ParTreeVisualizer.WordAlignement;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.print.PrintException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;



/**
 *
 * @author mwh
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, FileNotFoundException, PrintException {
        // TODO code application logic here

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
//
        File f = new File("/opt/uplug/test/1047daen.links8");
        Document doc = builder.parse(f);
        Element root = doc.getDocumentElement();
        System.err.println(root);
//        NamedNodeMap nm = root.getAttributes();
//        Node n = nm.item(0);
//        System.err.println(n);
//        
        NodeList linkGrps = root.getElementsByTagName("linkGrp");
        Element linkGrp = (Element) linkGrps.item(0);
        String toDoc = linkGrp.getAttribute("toDoc");
        String fromDoc = linkGrp.getAttribute("fromDoc");

        File toFile = new File("/opt/uplug/test/" + toDoc);
        File fromFile = new File("/opt/uplug/test/" + fromDoc);
        Document tD = builder.parse(toFile);
        Document fD = builder.parse(fromFile);

        System.err.println(toDoc + "  " + fromDoc);

        ParallelTreeVisualizer pt = new ParallelTreeVisualizer();

        NodeList links = root.getElementsByTagName("link");

        List<SentenceAlignement> sal = new ArrayList<SentenceAlignement>();
        
        for (int i = 0; (i < links.getLength()) && (i<100); i++) {
            Element link = (Element) links.item(i);
            String xtargetsStr = link.getAttribute("xtargets");

            String[] xtargets = xtargetsStr.split(";");
            String[] s1 = xtargets[0].split(" ");
            String[] s2 = xtargets[1].split(" ");
     //       System.err.println(Arrays.toString(s1) + "  " + Arrays.toString(s2));

            List<String> sentence1 = new ArrayList<String>();
            List<String> sentence2 = new ArrayList<String>();

            readSentences(tD, fD, s1, s2, sentence1, sentence2);

            List<WordAlignement> alignement = new ArrayList<WordAlignement>();
            
            NodeList wordLinks = link.getElementsByTagName("wordLink");
            for (int j = 0; j < wordLinks.getLength(); j++) {
                Element wordLink = (Element) wordLinks.item(j);

                Set<Integer> wa1 = new HashSet<Integer>();
                Set<Integer> wa2 = new HashSet<Integer>();
                
                
                String wordXtargetsStr = wordLink.getAttribute("xtargets");
                String[] wordXtargets = wordXtargetsStr.split(";");
                String[] w1 = wordXtargets[0].split("\\+");
                String[] w2 = wordXtargets[1].split("\\+");
                for (String w : w1) {
                    String[] wl = w.split("\\.");
                    wa1.add(Integer.parseInt(wl[2])-1);
                    System.err.println("wl1 " + wl[2]);
                }
                
                for (String w : w2) {
                    String[] wl = w.split("\\.");
                    wa2.add(Integer.parseInt(wl[2])-1);
                    System.err.println("wl2 " + wl[2]);
                }
                
                alignement.add(new WordAlignement(wa1, wa2));
            //    System.err.println("   " + Arrays.toString(w1) + "  " + Arrays.toString(w2));
             
            }
   System.err.println("---------------------------");
            SentenceAlignement sa = new SentenceAlignement(sentence1, sentence2, alignement);
            
            sal.add(sa);
            
          
            
            
        }

pt.visualize(sal);
    }


    public static void readSentences(Document tD, Document fD, String[] s1ids, String[] s2ids, List<String> s1, List<String> s2) {

        readSentence(fD, s1ids, s1);
        readSentence(tD, s2ids, s2);
        
    }
     
    public static void readSentence(Document D, String[] sids, List<String> ss) {
        
        Element root = D.getDocumentElement();

        // First read sentences in file1
        for (String s : sids) {
            Pattern pattern = Pattern.compile("s(\\d+).\\d+");
            Matcher matcher = pattern.matcher(s);
            String parNum = "";
            if (matcher.matches()) {
                parNum = matcher.group(1);
            }
            boolean foundPar = false;
            Element par=null;
            
            NodeList paragraphs = root.getElementsByTagName("p");
            for (int i=0; (i<paragraphs.getLength()) && !foundPar; i++) {
                Element curPar = (Element) paragraphs.item(i); 
                if (curPar.getAttribute("id").equals(parNum) ) {
                    par=curPar;
                    foundPar=true;
                }
            }
            
            Element sen=null;
            boolean foundSen = false;
            
            NodeList sentences = par.getElementsByTagName("s");
            for (int i=0; (i<sentences.getLength()) && ! foundSen; i++) {
                Element curSen = (Element) sentences.item(i);
                if (curSen.getAttribute("id").equals(s)) {
                    sen=curSen;
                    foundSen=true;
                }
            }
            System.err.println(foundPar + " " + foundSen);
            
            NodeList words = sen.getElementsByTagName("w");
            
            for (int i=0; i<words.getLength(); i++) {
                Element wordE = (Element) words.item(i);
                Text word = (Text) wordE.getFirstChild();
                String w = word.getData().trim();
                
                ss.add(w);
            }
            
            
        }

    }
}
